$(function () {
    toggleLoading(true);
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var originalHref = $('#doc-header a').attr('href');
    $('#doc-header a').attr('href', originalHref + '?hotel=' + currentHotel);
    originalHref = $('#doc-content a').attr('href');
    $('#doc-content a').attr('href', originalHref + '?hotel=' + currentHotel);
    toggleLoading(false);

    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
    }

    var requireLoginErrorHandler = function (error) {
        toggleLoading(false);
        if (typeof error.responseJSON === 'undefined') {
            toastr.error(lang('tip.system'));
            return;
        }

        if (error.responseJSON.status === 304) {
            $('#doc-body').addClass('hidden');
            $('#doc-content').removeClass('hidden');
            return;
        }
        toastr.error(error.responseJSON.info);
    };

    var requestCards = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Card/item', {
            'status': 0
        }, 'post', function (data) {
            toggleLoading(false);
            var cardInfos = data.data.card;
            cardInfos.reverse();
            var cardsHTML = '';
            if (cardInfos.length === 0) {
                cardsHTML = $('#templateRoomEmpty').clone().html();
            } else {
                var templateRoomHTML = $($('#templateRoom').clone().html());
                $.each(cardInfos, function (key, cardInfo) {
                    templateRoomHTML.attr('href', '/Home/RoomPage/index?hotel=' + currentHotel + '&no=' + cardInfo.orderRoomId+ '&UserId=' + cardInfo.bookUserId);
                    templateRoomHTML.find('.room-cover').attr('data-original', cardInfo.roomTypeCoverImageUrl);
                    templateRoomHTML.find('.room-hotel-name').html(cardInfo.hotelName);
                    templateRoomHTML.find('.room-room-type').html(cardInfo.roomTypeName);
                    templateRoomHTML.find('.room-room-name').html(cardInfo.roomName);
                    templateRoomHTML.find('.room-checkin-date').html(cardInfo.startDate);
                    templateRoomHTML.find('.room-checkout-date').html(cardInfo.endDate);
                    cardsHTML += templateRoomHTML.prop('outerHTML');
                });
            }
            $('#doc-body').html(cardsHTML);
            $('.room-cover').lazyload({
                skip_invisible: true
            });
        }, requireLoginErrorHandler);
    };

    requestCards();

    $('#doc-body').on('click', '.room-refresh', function (e) {
        e.preventDefault();
        requestCards();
    });
});
